<?php
/**
 * Created by PhpStorm
 * User:Ａdminstrator
 * Data: 2017-15-05
 * Time:13:21
 */
namespace App\Libs;

use App\Menu;
use Illuminate\Http\Request;
class CommonUtils {
    public $menu = null;//菜单对象

    public function __construct(Request $request)
    {
        $this -> init($request);
    }

    /**
     * @param 初始化函数
     */
    public function init(Request $request)
    {
        $this->menu = $this -> getMenu($request);
    }
    public function getMenu(Request $request)
    {
        $datas = Menu::orderBy('sort','asc')->where('status','0')->get()->toArray();
        if(empty($datas)){
            return '';
        }
        $menus = [];
        foreach ($datas as $key=>$v){
            //判断是否是顶级菜单,如果是顶级菜单则查出他下面对应的所有子菜单;
            if($v['pid'] == '0'){
                $menus[$key] = $v;
                $menus[$key]['child'] = Menu::where('pid',$v['id'])->get();
            }
        }

        return $menus;
    }
}